﻿using MigraDoc.DocumentObjectModel;
using MigraDoc.Rendering;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SwiftDocument
{
	/// <summary>
	/// Lógica de interacción para MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();
		}

		private void btnRender_Click(object sender, RoutedEventArgs e)
		{
		//	var document = new Document();
		//	var s = document.AddSection();
		//	s.AddParagraph(txtMarkdown.Text);
		//	var render = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
		//	render.Document = document;
		//	render.RenderDocument();
		//	var file = @"C:\Users\JairoAndres\Documents\file.pdf";
		//	render.PdfDocument.Save(file);
		//	Process.Start(file);
	
			var text = txtMarkdown.Text;
			var compiler = new Markdown.Markdown();
			var doc = (Model.Document)compiler.Parse(text);
			var odoc = new Document();			
			var global_section = odoc.AddSection();

			var fs = odoc.Styles.Cast<MigraDoc.DocumentObjectModel.Style>().FirstOrDefault(x => x.Name == StyleNames.Heading1);
			fs.ParagraphFormat.SpaceAfter = Unit.FromCentimeter(2);
			fs.ParagraphFormat.Font = new Font("Segoe UI Light", Unit.FromCentimeter(2.5));

			foreach (var block in doc.Blocks)
			{
				if (block is Model.Header)
				{
					var header = block as Model.Header;
					//var par = global_section.AddParagraph(header.Content);
					//par.Style = MigraDoc.DocumentObjectModel.StyleNames.Heading1;
				}
				else if (block is Model.Paragraph)
				{
					var paragraph = block as Model.Paragraph;					
					//var p = string.Join(" ", paragraph.Lines.Select(x => x.Text));
					//var par = global_section.AddParagraph(p);
					//par.Style = StyleNames.Normal;
				}
			}
			var render = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
			render.Document = odoc;
			render.RenderDocument();
			var file = @"C:\Users\JairoAndres\Documents\file.pdf";
			render.PdfDocument.Save(file);
			Process.Start(file);
		}
	}
}
